/**
 * Created by Dalyn on 2017/7/10.
 */
import React , {Component}from 'react';
import {Icon,message} from 'antd';
import {Modal,Button,Input} from 'fly';
import Util from "../../util/Util"

export default class WorkShopModal extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            workShopFlag:false,
            workShopName:"",//车间名
            workShopHint:"",//input提示
        };
        this.operateFlag = true;
        this.messageFlag = true;
    }
    showModal(flag){
        this.setState({
            workShopFlag: flag,
        });
    }
    /*取消*/
    workShopCancel(){
        this.setState({
            workShopFlag: false,
            workShopName: "",
            workShopHint: "",
        });
    }
    /*确定*/
    workShopSure(){
        let params = {};
        let {workShopName} = this.state;
        let {workShopId,workShopType} = this.props;
        if(workShopType == "create"){//添加
            workShopName = workShopName.replace(/(^\s*)|(\s*$)/g,"");//去除首尾空格
            if(!workShopName){
                // message.warning("车间名称不能为空！");
                this.setState({
                    workShopHint:"车间名称不能为空",
                });
                return
            }
            if(this.operateFlag){
                this.operateFlag = false;
                params.workShopName = workShopName;
                this.props.workShopOperate("create",params);
            }else {
               // message.warning("不能频繁操作")
            }
        }else if(workShopType == "edit"){//修改
            /*校验？？？*/
            workShopName = workShopName.replace(/(^\s*)|(\s*$)/g,"");//去除首尾空格
            if(!workShopName){
                // message.warning("车间名称不能为空！");
                this.setState({
                    workShopHint:"车间名称不能为空",
                });
                return
            }
            if(this.operateFlag){
                this.operateFlag = false;
                params.workShopId = workShopId;
                params.workShopName = workShopName;
                this.props.workShopOperate("edit",params);
            }else {
                //message.warning("不能频繁操作")
            }
        }else if(workShopType == "delete"){//删除
            params.workShopId = workShopId;
            this.props.workShopOperate("delete",params);
        }
    }
    /*输入框*/
    workShopName(e){
        let this_ = this;
        let value = e.target.value;
        //console.dir(e.target);
        /*if(value.length>6){
            Util.messageHint(this_,"error","不能超过6个字！");
            // e.target.value = value.toString(0,6);
            return
        }*/
        this.setState({
            workShopName:value,
            workShopHint:"",
        })
    }
    render(){
        let {workShopName,workShopHint} = this.state;
        let {workShopType,workShopHintText,workShopId} = this.props;
        let inputClass = workShopHint?"production_facResource_wsModal_name input_border_red":"production_facResource_wsModal_name";
        //console.log( workShopName);
        let workShopTitle = ()=>{
            if(workShopType == "create"){
                return "创建车间"
            }else if(workShopType == "edit"){
                return "修改"
            }else if(workShopType == "delete"){
                return "提示"
            }
        };
        let modal_content = ()=>{
            if(workShopType == "create"){
                return (
                    <p className="production_facResource_wsModal_left">
                        <span className="production_facResource_wsModal_word">车间名称：</span>
                        <Input
                            type="text" ref={"workShopName"}
                            className={inputClass}
                            value={workShopName}
                            onChange={this.workShopName.bind(this)}
                            maxLength="6"
                        />
                        <p className="production_facResource_wsModal_spanHint">{workShopHint}</p>
                    </p>
                )
            }else if(workShopType == "edit"){
                return (
                    <p className="production_facResource_wsModal_left">
                        <span className="production_facResource_wsModal_word">车间名称：</span>
                        <Input
                            type="text" ref={"workShopName"}
                            className={inputClass}
                            value={this.state.workShopName}
                            onChange={this.workShopName.bind(this)}
                            maxLength="6"
                        />
                        <p className="production_facResource_wsModal_spanHint">{workShopHint}</p>
                    </p>
                )
            }else if(workShopType == "delete"){
                return (
                    <p className="production_facResource_wsModal_hint_text">
                        <Icon type="exclamation-circle"  className="production_facResource_wsModal_warning"/>
                        <span className="production_facResource_wsModal_hint_word">{workShopHintText}</span>
                    </p>
                )
            }
        };
        return (
            <Modal
                title={workShopTitle()}
                visible={this.state.workShopFlag}
                onCancel={this.workShopCancel.bind(this)}
                onOk={this.workShopSure.bind(this)}
                //key={workShopType}
            >
                {
                    modal_content()
                }
            </Modal>
        )
    }
}